package com.interior.study.controller;

import com.interior.study.pojo.Employees;
import com.interior.study.service.EmployeesService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;

@Controller
public class EmployeesController {

    @Resource
    private EmployeesService employeesService;

    @RequestMapping("/staff")
    public String staff(Model model){
        List<Employees> Emplist=employeesService.findEmpListAll();
        model.addAttribute("Emplist",Emplist);
        return "button";
    }
    @GetMapping(value = "index.do")
    public ModelAndView index() {
        return new ModelAndView("login"); // 此处指向界面
    }

    @GetMapping(value = "login.do")
    public Object login(HttpServletRequest request, String employeesid, String password) {
        System.out.println("传入参数：name=" + employeesid + ", password=" + password);
        request.setAttribute("name",employeesid);
        request.setAttribute("password",password);
        if (StringUtils.isEmpty(employeesid)) {
            request.setAttribute("zt","name不能为空");
            return new ModelAndView("login");
        } else if (StringUtils.isEmpty(password)) {
            request.setAttribute("zt","password不能为空");
            return new ModelAndView("login");
        }
        Employees e = employeesService.findBylogin(employeesid, password);
        if (e != null) {
            request.getSession().setAttribute("name",e.getName());
            return new ModelAndView("index");
        } else {
            request.setAttribute("zt","用户名或密码错误");
            return new ModelAndView("login");
        }
    }

}
